"""
演示全国疫情可视化地图开发
"""
import os
import json
from pyecharts.charts import Map
from pyecharts.options import *

current_path = os.getcwd()

# 读取数据文件
try:
    f = open(current_path + "/疫情.txt", "r", encoding="UTF-8")
    json_str = f.read()
except Exception as e:
    print(f"读取数据出错：{e}，错误类型：{type(e)}")
else:
    f.close()

# 取到各省的数据
data_dict = json.loads(json_str)
province_data_list = data_dict['areaTree'][0]['children']
# 组装每个省份和确诊人数的元组，并各个省份的数据都封装到列表内
map_data = []  # 绘图需要用的数据
for temp in province_data_list:
    name = temp['name']  # 省份名称
    count = temp['total']['confirm']  # 确诊人数
    map_data.append((name, count))

# 构建地图对象
map = Map()
# 添加数据
map.add("各省份确诊人数", map_data, "china")
# 设置全局选项
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图", pos_left="center", pos_bottom="1%"),
    visualmap_opts=VisualMapOpts(
        is_show=True,  # 是否显示
        is_piecewise=True,  # 是否分段
        pieces=[
            {"min": 1, "max": 99, "label": "1-99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100-999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000-4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "5000-9999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000-99999人", "color": "#CC3333"},
            {"min": 100000, "label": "100000人以上", "color": "#990033"}
        ]
    )
)
# 绘图
map.render(path="全国疫情可视化地图.html")
